﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace MNNUTEM
{
    public partial class Productos : Form

    {
        private int idSucursal=1,idProducto=1,sw=0;
        private crudProducto cProducto;

        public Productos()
        {
            InitializeComponent();
            cProducto = new crudProducto();
        }

        private void updateGridView()
        {
            dataGridView1.DataSource = cProducto.Consultation("select *from Producto");

        }

        private void Productos_Load(object sender, EventArgs e)
        {
            button4.Enabled = false;
            button3.Enabled = false;
            button1.Enabled = false;
            textBox14.Enabled = false;
            dataGridView1.DataSource = cProducto.Consultation("select *from Producto");
            dataGridView3.DataSource = cProducto.Consultation("select *from Sucursal");
            updateGridView();
            woStock();
           
        }

      


         public void woStock()
        {
            cProducto.Consultation3(dataGridView1);
          
            
         }
       
        private void button1_Click(object sender, EventArgs e)
        {
           
        }

      

        private void button5_Click(object sender, EventArgs e)
        {
            DataSet DS;
            DS = cProducto.Consultation2("select arriendoSucursal from Sucursal where idSucursal='"+idSucursal+"'");
          //  textBox12.Text = DS.Tables[0].Rows[0][0].ToString(); 
        }

        private void clean()
        {
            
            button3.Enabled = false;
            button4.Enabled = false;
            if(sw==1)
            button1.Enabled = true;

            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
            textBox8.Text = "";
            
        }
        private void getdatos()
        {
            DataSet DS;
            DS = cProducto.Consultation2("select *from Producto where idProducto='" + idProducto + "'");
            textBox1.Text = DS.Tables[0].Rows[0][1].ToString();
            textBox2.Text = DS.Tables[0].Rows[0][0].ToString();
            textBox3.Text = DS.Tables[0].Rows[0][2].ToString();
            textBox4.Text = DS.Tables[0].Rows[0][3].ToString();
            textBox5.Text = DS.Tables[0].Rows[0][4].ToString();
            textBox6.Text = DS.Tables[0].Rows[0][7].ToString();
            textBox8.Text = DS.Tables[0].Rows[0][8].ToString();
            textBox14.Text = DS.Tables[0].Rows[0][9].ToString();

        }

        private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            
          

        }

        private void button2_Click(object sender, EventArgs e)
        {
           
        }

        private void updateId(int idProducto)
        {

            DataSet DS;
            DS = cProducto.Consultation2("select count(idProducto) from Producto");
            int count = int.Parse(DS.Tables[0].Rows[0][0].ToString())+1;
           
            if (idProducto != count)
                for (int i = idProducto; i <= count; i++)
                {
                    
                    cProducto.SetId(i, i + 1);
                }
            else { }
        
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
            
        }

        private void dataGridView3_RowHeaderMouseDoubleClick_1(object sender, DataGridViewCellMouseEventArgs e)
        {
          
        }

        private void button4_Click(object sender, EventArgs e)
        {
            cProducto.Set(textBox2, textBox1, textBox3, textBox4, textBox5, textBox6, textBox8, int.Parse(textBox14.Text),textBox7,textBox10);
            updateGridView();
        }

        private void dataGridView1_RowHeaderMouseDoubleClick_1(object sender, DataGridViewCellMouseEventArgs e)
        {
            idProducto = int.Parse(dataGridView1[0, e.RowIndex].Value.ToString());
            getdatos();
            button1.Enabled = false;
            button3.Enabled = true;
            button4.Enabled = true;
            tabControl1.SelectedTab = tabPage2;
        }

        private void dataGridView3_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            idSucursal = int.Parse(dataGridView3[0, e.RowIndex].Value.ToString());
            textBox14.Text = idSucursal.ToString();
            button1.Enabled = true;
            sw = 1;
            tabControl1.SelectedTab = tabPage2;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            cProducto.InsertProductoCostoLote(textBox2, textBox1, textBox3, textBox4, textBox5, textBox6, textBox8, textBox14, textBox7, textBox10);
            //   cProducto.Insert(textBox2,textBox1,textBox3,textBox4,textBox5);
            updateGridView();
            clean();
            Costos costo = new Costos();
            costo.Show();
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            clean();
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            cProducto.Delete(idProducto);
            updateId(idProducto);
            updateGridView();

            clean();
        }

        private void textBox9_TextChanged(object sender, EventArgs e)
        {
            string filtro = textBox9.Text;
            dataGridView1.DataSource = cProducto.Consultation("SELECT * FROM Producto WHERE nombreProducto LIKE '%" + filtro + "%' ");
            woStock();
        }
    }
}


        